<template>
  <div class="app-container">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px" :inline="true">
      <el-form-item label="任务编号" prop="name">
        <el-input v-model="seriesIoForm.channel" class="three_input three_input_mragin" disabled></el-input>
      </el-form-item>
      <el-form-item label="数据类型" prop="dataType">
        <el-select v-model="form.dataType" placeholder="请选择数据类型" class="three_input three_input_mragin">
          <el-option label="位（bit）" value="0">位（bit）</el-option>
          <el-option label="大端 16 位无符号" value="3">大端 16 位无符号</el-option>
          <el-option label="大端 16 位有符号" value="4">大端 16 位有符号</el-option>
          <el-option label="大端 32 位无符号" value="5">大端 32 位无符号</el-option>
          <el-option label="大端 32 位有符号" value="6">大端 32 位有符号</el-option>
          <el-option label="大端 32 位浮点数" value="7">大端 32 位浮点数</el-option>
          <el-option label="大端 64 位无符号" value="8">大端 64 位无符号</el-option>
          <el-option label="大端 64 位有符号" value="9">大端 64 位有符号</el-option>
          <el-option label="大端 64 位浮点数" value="10">大端 64 位浮点数</el-option>
          <el-option label="小端 16 位无符号" value="11">小端 16 位无符号</el-option>
          <el-option label="小端 16 位有符号" value="12">小端 16 位有符号</el-option>
          <el-option label="小端 32 位无符号" value="13">小端 32 位无符号</el-option>
          <el-option label="小端 32 位有符号" value="14">小端 32 位有符号</el-option>
          <el-option label="小端 32 位浮点数" value="15">小端 32 位浮点数</el-option>
          <el-option label="小端 64 位无符号" value="16">小端 64 位无符号</el-option>
          <el-option label="小端 64 位有符号" value="17">小端 64 位有符号</el-option>
          <el-option label="小端 64 位浮点数" value="18">小端 64 位浮点数</el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="数据长度" prop="bitlen">
        <el-input-number v-model="form.bitlen" :min="0" class="three_input" :disabled="form.dataType!=0"/>
      </el-form-item>
      <el-form-item label="采样周期" prop="cycle">
        <el-input-number v-model="form.cycle" :min="0" class="three_input"/> 100ms
      </el-form-item>
      <el-form-item label="应答等待" prop="ackWait">
        <el-input-number v-model="form.ackWait" :min="0" class="three_input"/> &nbsp; ms
      </el-form-item>
      <el-form-item label="失败次数" prop="ackFail>">
        <el-input-number v-model="form.ackFail" :min="0" class="three_input"/> &nbsp; 
      </el-form-item>
      <el-divider content-position="center">Modbus 协议参数</el-divider>
      <el-form-item label="从站地址" prop="addr">
        <el-input v-model="form.addr" :min="0" class="three_input three_input_mragin"/>
      </el-form-item>
      <el-form-item label="功能码" prop="code">
        <el-select v-model="form.code" placeholder="请选择数据类型" class="three_input three_input_mragin">
          <el-option label="01H,05H,15H 功能码，读写线圈" value="0">01H,05H,15H 功能码，读写线圈</el-option>
          <el-option label="02H 功能码，读输入离散量" value="1">02H 功能码，读输入离散量</el-option>
          <el-option label="02H 功能码，读输入离散量" value="2">04H 功能码，读输入寄存器</el-option>
          <el-option label="03H,06H,10H 功能码，读写保存寄存器" value="3">03H,06H,10H 功能码，读写保存寄存器</el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="寄存器起始地址(十进制)" prop="regadd">
        <el-input v-model="form.regadd" :min="0" />
      </el-form-item>
      <el-divider content-position="center">数据上传方式</el-divider>
      <el-form-item label="" prop="updly" class="three_input_inline">
        <el-checkbox v-model="updly" label="1" class="three_radio">定时</el-checkbox> &nbsp;&nbsp;
        <el-input-number v-model="form.updly" :min="0" :disabled="!updly" class="three_input"/>&nbsp;&nbsp;s
      </el-form-item>
      <el-form-item label="" prop="upchange" class="three_input_inline">
        <el-checkbox v-model="upchange" label="2" class="three_radio">数值变化</el-checkbox> &nbsp;&nbsp;
        <el-input v-model="form.upchange" :disabled="!upchange" class="three_input three_input_mragin"/> &nbsp;&nbsp; 
      </el-form-item>
      <el-form-item label="" prop="threshold" class="three_input_inline">
        <el-checkbox v-model="threshold" label="4" class="three_radio">阈值触发</el-checkbox> &nbsp;&nbsp;
        低阈值：<el-input v-model="form.upthlow" :disabled="!threshold" class="three_input_180 three_input_mragin"/> &nbsp;&nbsp; 
        高阈值：<el-input v-model="form.upthhigh" :disabled="!threshold" class="three_input_180 three_input_mragin"/> &nbsp;&nbsp; 
        持续时间：<el-input v-model="form.upthkeep" :disabled="!threshold" class="three_input_180"/> &nbsp;&nbsp; * 100ms
      </el-form-item>
    </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
  </div>
</template>

<script>
import code from '../cmdData/code'
export default {
  props:{
    deviceId:'',
    seriesIoForm:null,
    sensorList:null,
    controllerList:null
  },
  dicts: ['cmd_execute_status'],
  components:{'v-code':code},
  data() {
    return {
      // 总条数
      model:1,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      updly:false,
      upchange:false,
      threshold:false,
      // data:'1,3,0,10,采样频率,失败次数,1,定时上传,0,0,0,0,3,3,寄存器地址',
      data:'model,dataType,bitlen,cycle,ackWait,ackFail,uploadModel,updly,upchange,upthlow,upthhigh,upthkeep,addr,code,regadd',
      // 表单参数
      form: {
        dataType:'3',
        bitlen:'0',
        cycle:'10',
        ackWait:'100',
        ackFail:'5',
        addr:'3',
        code:'3',
        regadd:'0',
        model:"1",
        updly:'60', //定时上传时间  单位秒
        upchange:'0',
        upthlow:'0',
        upthhigh:'0',
        upthkeep:'0'
      },
      // 表单校验
      rules: {
      },
    };
  },
  created() {
    this.initForm();
  },
  methods: {
    initForm(){
      if(this.seriesIoForm.commandList && this.seriesIoForm.commandList.length > 0){
        let value = this.seriesIoForm.commandList.length == 3 ? this.seriesIoForm.commandList[1].value:this.seriesIoForm.commandList[0].value;
        if(!value) return false;
        let values = value.split(',');
        this.form.dataType = values[1];
        this.form.bitlen = values[2];
        this.form.cycle = values[3];
        this.form.ackWait = values[4];
        this.form.ackFail = values[5];
        this.form.model = values[6];
        this.form.updly = values[7];
        this.form.upchange = values[8];
        this.form.upthlow = values[9];
        this.form.upthhigh = values[10];
        this.form.upthkeep = values[11];
        this.form.addr = values[12];
        this.form.code = values[13];
        this.form.regadd = values[14];
        if(this.form.model == 1) this.updly = true ;
        if(this.form.model == 2) this.upchange = true ;
        if(this.form.model == 3) {this.upchange = true ; this.updly = true };
        if(this.form.model == 4) {this.threshold = true };
        if(this.form.model == 5) {this.threshold = true ; this.updly = true };
        if(this.form.model == 6) {this.threshold = true ; this.upchange = true };
        if(this.form.model == 7) {this.threshold = true ; this.upchange = true ; this.updly = true };
      }
    },
    // 取消按钮
    cancel() {
      this.$emit("cancel",null)
    },
    /** 提交按钮 */
    submitForm() {
      let form = this.form;
      //data:'model,dataType,bitlen,cycle,ackWait,ackFail,uploadModel,fixedTime,upchange,upthlow,upthhigh,upthkeep,addr,code,regadd',
     let upModel = 0;
     if(this.updly) upModel = 1;
     else this.form.updly = 0;
     if(this.upchange) upModel = upModel+2;
     else this.form.upchange = 0;
     if(this.threshold) upModel = upModel+4;
     else {
      this.form.upthlow = 0 ;
      this.form.upthhigh = 0;
      this.form.upthkeep = 0 ;
     }
     let obj = {};
     let data = `1,${form.dataType},${form.bitlen},${form.cycle},${form.ackWait},${form.ackFail},${upModel},${form.updly},${form.upchange},${form.upthlow},${form.upthhigh},${form.upthkeep},${form.addr},${form.code},${form.regadd}`;
     obj.commandList = [{name:'更新数据',value:'?'},{name:'添加任务',value:data},{name:'删除任务',value:'0'}];
     obj.valueExpression = `
        function valueConvert(data){
          if(data.param.value1 == '?')
          data.cmdFlag = data.cmdFlag.replace('GWTASK','GWTASKDT')
        }
     `;
     this.$emit("success",obj)
    }
  },
  watch:{
    upchange(val){
      console.log(val)
    },
    "form.dataType"(val){
      if(val!=0) this.form.bitlen = 0;
    }
  }
};
</script>
<style scoped>
.three_input{
  width:210px
}
.three_radio{
  width:80px
}
.three_input_180{
  width:180px
}
.three_input_mragin{
  margin-right:35px
}
.three_input_inline{
  display: inline;
  /* line-height: 32px; */
}
</style>